package com.tianji.jersey.jaxb;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.jersey.api.json.JSONJAXBContext;
import com.tianji.jersey.jaxb.bean.FlightType;
import com.tianji.jersey.jaxb.bean.Flights;

/**
 *
 * @author japod
 */
//@Provider
public final class JAXBContextResolver implements ContextResolver<JAXBContext> {
    private Log log = LogFactory.getLog(this.getClass());
    private final JAXBContext context;
    
    private final Set<Class> types;
    
    private final Class[] cTypes = {Flights.class, FlightType.class};
    
    public JAXBContextResolver() throws Exception {
    	log.debug("====================================JAXBContextResolver");
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(JSONJAXBContext.JSON_NOTATION, JSONJAXBContext.JSONNotation.MAPPED);
        props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING, Boolean.TRUE);
        props.put(JSONJAXBContext.JSON_NON_STRINGS, new HashSet<String>(1){{add("number");}});
        this.types = new HashSet(Arrays.asList(cTypes));
        this.context = new JSONJAXBContext(cTypes, props);
    }
    
    public JAXBContext getContext(Class<?> objectType) {
    	log.debug("====================================getContext");
        return (types.contains(objectType)) ? context : null;
    }
}
